//
//  ZTLinkageUtil.m
//  YSSmartHome
//
//  Created by Yahaong on 16/9/5.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "ZTLinkageUtil.h"
#import "MQTTSessionManager.h"
#import "DataManager.h"
#import <XAISDK/XAIFormatDataCount.h>
#import "GlobalConst.h"

@interface ZTLinkageUtil ()

@end
@implementation ZTLinkageUtil


+ (void)modifLinkageConds:(NSArray <XAILinkItemDes *>*)conds actions:(NSArray <XAILinkItemDes *>*)actions status:(ZTLinkageStatus)status name:(NSString*)name formula:(NSString *)formula linkageNum:(uint8_t)linkagenum
{
    XAIFormatDataCount* formatCount = [[XAIFormatDataCount alloc]init];
    uint8_t flag = linkagenum;
    [formatCount addDataType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&flag length:sizeof(flag)];
    [formatCount addDataType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&status length:sizeof(status)];
    [formatCount addDataType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[[name dataUsingEncoding:NSUTF8StringEncoding] bytes] length:(int)[name dataUsingEncoding:NSUTF8StringEncoding].length];
    NSString *strFormula = @"00";
    [formatCount addDataType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[[strFormula dataUsingEncoding:NSUTF8StringEncoding] bytes] length:(int)[strFormula dataUsingEncoding:NSUTF8StringEncoding].length];
    
    for (int i = 0; i < conds.count; i++)
    {
        XAILinkItemDes *linkItemDes = conds[i];
        NSData* data =  [linkItemDes toData];
        [formatCount addDataType: XAI_DATA_TYPE_BIN_LINK_TRIGGER bytes:(void*)[data bytes] length:(int)data.length];
    }
    for (int i = 0; i < actions.count; i++)
    {
        XAILinkItemDes *linkItemDes = actions[i];
        NSData* data =  [linkItemDes toData];
        [formatCount addDataType: XAI_DATA_TYPE_BIN_LINK_ACTION bytes:(void*)[data bytes] length:(int)data.length];
    }
    [kMQTTSessionManager sendCmd:0x03 toApsn:kMQTTSessionManager.currentUser.currentBindApsn toLuid:kGATEWAYLUID data:formatCount];
    
}

/**
 * 添加联动到指定网关
 *
 * @param apsn
 *            待添加到的网关编号
 * @param condition
 *            联动条件组
 * @param action
 *            联动动作组
 * @return ERRNO
 */

+ (void) linkAdd:(XAITYPEAPSN )apsn  name:(NSString*) name conditions:
(NSArray <ZTLinkageInfo*> *)conditions actions: (NSArray <ZTLinkageInfo*> *) actions
{
//    XAIFormatDataCount* formatCount = [[XAIFormatDataCount alloc]init];
//    uint8_t status = 0;
//    [formatCount addDataType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&status length:sizeof(status)];
//    uint8_t active = ZTLinkageStatus_Active;
//    [formatCount addDataType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&active length:sizeof(active)];
//    [formatCount addDataType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[[name dataUsingEncoding:NSUTF8StringEncoding] bytes] length:(int)[name dataUsingEncoding:NSUTF8StringEncoding].length];
//    NSString *strFormula = @"00";
//    [formatCount addDataType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[[strFormula dataUsingEncoding:NSUTF8StringEncoding] bytes] length:(int)[strFormula dataUsingEncoding:NSUTF8StringEncoding].length];
    NSMutableArray* conds =  [conditions valueForKey:@"linkItemDes"];
    NSMutableArray* acts =  [actions valueForKey:@"linkItemDes"];
//    for (int i = 0; i < conditions.count; i++)
//    {
//        XAILinkItemDes *linkItemDes = conditions[i].linkItemDes;
//        NSData* data =  [linkItemDes toData];
//        [formatCount addDataType: XAI_DATA_TYPE_BIN_LINK_TRIGGER bytes:(void*)[data bytes] length:(int)data.length];
//    }
//    for (int i = 0; i < actions.count; i++)
//    {
//        XAILinkItemDes *linkItemDes = actions[i].linkItemDes;
//        NSData* data =  [linkItemDes toData];
//        [formatCount addDataType: XAI_DATA_TYPE_BIN_LINK_ACTION bytes:(void*)[data bytes] length:(int)data.length];
//    }
//    [kMQTTSessionManager sendCmd:0x03 toApsn:kMQTTSessionManager.currentUser.currentBindApsn toLuid:kGATEWAYLUID data:formatCount];
    
    NSError* error =  [kMQTTSessionManager.xai linkAdd:apsn name:name conditions:conds actions:acts];
    if (error.code == 0) {
        debugLogInfo(@"联动添加成功");
    }
    else
    {
        debugLogInfo(@"%@",error.localizedDescription);
    }

}
+ (void)findAllLinkages:(XAITYPEAPSN) apsn luid:(XAITYPELUID)luid
{
//    NSString *topic = [MQTTCoverTool gatewayConfigLinkageTopicWithAPNS:apsn luid:luid];
//    [kMQTTSessionManager.mqttClient subscribe:topic withQos:ExactlyOnce];
}

+ (void) setLinkage:(LinkageNum)linkNum status:(ZTLinkageStatus)linkageStatus
{
    LinkageNum num = linkNum;
    NSMutableArray* datas = [NSMutableArray array];
    XAITLV* tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&num length:sizeof(num)];
    [datas addObject:tlv];
    NSError* error =  [kMQTTSessionManager.xai sendCMD:0x02 toApsn:kMQTTSessionManager.currentUser.currentBindApsn toLuid:kGATEWAYLUID datas:datas];
    if (error.code == 0) {
        debugLogInfo(@"联动执行成功");
    }
    else
    {
        debugLogInfo(@"%@",error.localizedDescription);
    }
}

+ (void)delLinkage:(ZTLinkage *)linkage;
{
//    XAIFormatDataCount * formatDataCount = [[XAIFormatDataCount alloc]init];
      uint8_t flage = linkage.num;
//    [formatDataCount addDataType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&flage length:sizeof(flage)];
    ZTLinkageStatus status = ZTLinkageStatus_Del;
//    [formatDataCount addDataType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&status length:sizeof(status)];
//    
    NSData* nameData = [linkage.name dataUsingEncoding:NSUTF8StringEncoding];
//    [formatDataCount addDataType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[nameData bytes] length:(int)nameData.length];
    NSData  *formulaData = [linkage.formula dataUsingEncoding:NSUTF8StringEncoding];
//    [formatDataCount addDataType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[formulaData bytes] length:(int)formulaData.length];
//    [kMQTTSessionManager sendCmd:0x03 toApsn:kMQTTSessionManager.currentUser.currentBindApsn toLuid:kGATEWAYLUID data:formatDataCount];
    
    NSMutableArray* datas = [NSMutableArray array];
    XAITLV* tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&flage length:sizeof(flage)];
    [datas addObject:tlv];
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&status length:sizeof(status)];
    [datas addObject:tlv];
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[nameData bytes] length:sizeof((int)nameData.length)];
    [datas addObject:tlv];
    
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_ASCII_TEXT bytes:(void*)[formulaData bytes] length:(int)formulaData.length];
    [datas addObject:tlv];
    NSError* error = [kMQTTSessionManager.xai sendCMD:0x03 toApsn:kMQTTSessionManager.currentUser.currentBindApsn toLuid:kGATEWAYLUID datas:datas];
    if (error.code == 0) {
        debugLogInfo(@"删除成功");
    }
    else
    {
        debugLogInfo(@"%@",error.localizedDescription);
    }
}

@end
